import contentStore from "../utils/contentStore";
import tokenStore from "../utils/tokenStore";
import chatStore from "../utils/chatStore";

export const getBanner = async () => {
	return await new Promise(function(resolve, reject) {
		uni.request({
			// method: 'POST',
			url: 'https://zm.quya.vip/api/v1/getBannerList',
			success: (res) => {
				console.log(res)
				if (res.data.code == 200) {
					uni.setStorageSync('BannerList',
						res.data.data);
					console.log(uni.getStorageSync(
						'BannerList'));
					resolve(res.data.data)
				} else {
					uni.showToast({
						title: res.data.message || res.data.msg,
						icon: 'none'
					})
					reject(res.data)
				}
			}
		});
	});
}

export const getAiCate = async () => {
	return await new Promise(function(resolve, reject) {
		uni.request({
			// method: 'POST',
			url: 'https://zm.quya.vip/api/v1/getAiCateList',
			success: (res) => {
				console.log(res)
				if (res.data.code == 200) {
					uni.setStorageSync('AiCateList',
						res.data.data);
					console.log(uni.getStorageSync(
						'AiCateList'));
					resolve(res.data.data)
				} else {
					uni.showToast({
						title: res.data.message || res.data.msg,
						icon: 'none'
					})
					reject(res.data)
				}
			}
		});
	});
}

// https://zm.quya.vip/api/v1/getCateFunctionList


export const getCateFunction = async (query) => {
	let url ='https://zm.quya.vip/api/v1/getCateFunctionList?cate_id='+query.cate_id;
	query.page && ( url+=('&page='+query.page) );
	query.pageSize && ( url+=('&pageSize='+query.pageSize) );
	
	return await new Promise(function(resolve, reject) {
		uni.request({
			// method: 'POST',
			url,
			success: (res) => {
				console.log(res)
				if (res.data.code == 200) {
					resolve(res.data.data)
				} else {
					uni.showToast({
						title: res.data.message || res.data.msg,
						icon: 'none'
					})
					reject(res.data)
				}
			}
		});
	});
}
export const fabulous = async (query) => {
	let url ='https://zm.quya.vip/api/v1/fabulous';
	
	return await new Promise(function(resolve, reject) {
		uni.request({
			method: 'POST',
			url,
			header: {
				'Authorization': 'Bearer ' + tokenStore.get()
			},
			data:{
				id:query.id
			},
			success: (res) => {
				console.log(res)
				if (res.data) {
					resolve(res.data)
				}
			}
		});
	});
}
export const getArticleDetails = async (query) => {
	let url ='https://zm.quya.vip/api/v1/getArticleDetails?id='+query.id;
	
	return await new Promise(function(resolve, reject) {
		uni.request({
			// method: 'POST',
			url,
			success: (res) => {
				console.log(res)
				if (res.data.code == 200) {
					contentStore.set(res.data.data)
					resolve(res.data.data)
				} else {
					uni.showToast({
						title: res.data.message || res.data.msg,
						icon: 'none'
					})
					reject(res.data)
				}
			}
		});
	});
}
export const getFunctionDetails = async (query) => {
	let url ='https://zm.quya.vip/api/v1/getFunctionDetails?id='+query.id;
	
	return await new Promise(function(resolve, reject) {
		uni.request({
			// method: 'POST',
			url,
			success: (res) => {
				console.log(res)
				if (res.data.code == 200) {
					chatStore.set(res.data.data)
					resolve(res.data.data)
				} else {
					uni.showToast({
						title: res.data.message || res.data.msg,
						icon: 'none'
					})
					reject(res.data)
				}
			}
		});
	});
}